<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>
        Lorem ipsum dolor sit amet.
    </div>
    <script>
        //禁用浏览器 默认右键菜单
        document.oncontextmenu = new Function("return false");
    </script>
    <script>
        //禁止按F12
        document.onkeydown = function (event) {
            var e = event || window.event || arguments.callee.caller.arguments[0];

            if (e && e.keyCode == 123) {
                mAlert();
                e.returnValue = false;
                return (false);
            }
        }
        function mAlert() {
            // alert("禁止操作控制台或单击右键");
        }

        //鼠标点击右键的时候
        document.onmousedown = function mdClick(event) {
            var e = event || window.event || arguments.callee.caller.arguments[0];
            if (e.button == 2 || e.button == 3) {
                mAlert();
            }
        }
    </script>


    <!-- 双击就是复制 -->
    <div id="thisDiv" onclick="execClick()">这里是DIV的内容(双击就是复制)</div>
    <script>
        function execClick() {
            document.execCommand("copy")
        }
    </script>


    <p class="inviteCode" id="inviteCode">作者常相思</p>
    <button type="button" id="sub">复制</button>
    <input type="text" id="assgin">
    <input type="text">
    <input type="text">
    <input type="text">

    <script>
        document.getElementById('sub').onclick = function () {
            var Url2 = document.getElementById("inviteCode").innerText;
            var oInput = document.createElement("input");
            oInput.value = Url2;
            document.body.appendChild(oInput);
            oInput.select(); // 选择对象
            document.execCommand("Copy"); // 执行浏览器复制命令
            oInput.className = "oInput";
            oInput.style.display = "none";
        }

    </script>



    <script>
        var pasteText = '';
        document.body.oncopy = function (e) {
            e.preventDefault();//极其关键的一句话,不写,修改剪切板的内容,粘贴就会无效
            var textArr = window.getSelection().toString().split("");
            pasteText = '';
            textArr.forEach(function (e) {
                pasteText += e;
            });
            e.clipboardData.setData('text', '')

        }
        document.body.onpaste = function (e) {
            document.getElementsByTagName('input')[0].value = pasteText;
            document.getElementsByTagName('input')[1].value = pasteText;
            document.getElementsByTagName('input')[2].value = pasteText;
            document.getElementsByTagName('input')[3].value = pasteText;
        }
    </script>





    <style>
        .cont {
            width: 60%;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
        }

        .text {
            width: 60%;
            height: 40px;
            color: #000;
            line-height: 40px;
            font-size: 16px;
            border: 5px solid #ddd;
            padding: 0 5px;
            font-weight: bold;
            border-image: -webkit-linear-gradient(#ff4500, #ffa500) 20 20;
            border-image: -moz-linear-gradient(#ff4500, #ffa500) 20 20;
            border-image: -o-linear-gradient(#ff4500, #ffa500) 20 20;
            border-image: linear-gradient(#ff4500, #ffa500) 20 20;
        }

        .copy {
            width: 30%;
            height: 50px;
            line-height: 50px;
            background-color: #ff9800;
            color: #fff;
            border: none;
            font-size: 14px;
            text-align: center;
            text-align: center;
            background: -webkit-linear-gradient(left, #ff4500, #ffa500);
            background: -o-linear-gradient(right, #ff4500, #ffa500);
            background: -moz-linear-gradient(right, #ff4500, #ffa500);
            background: linear-gradient(to right, #ff4500, #ffa500);
        }
    </style>
    <div class="cont">
        <div class="text" contenteditable="true">需要复制的文字</div>
        <button type="button" class="copy">复制</button>
    </div>
    <script>
        //另一种方法
        $(".copy").click(function () {
            Copy($(".text").text())
        })
        //str 要复制的值
        function Copy(str) {
            console.log(str)
            var save = function (e) {
                e.clipboardData.setData('text/plain', str);
                e.preventDefault();
            };
            document.addEventListener('copy', save);
            document.execCommand('copy');
            document.removeEventListener('copy', save);
            console.log('复制成功');
        }
    </script>

</body>
</html>